﻿using System;
// using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace MobileTDL
{
    abstract public class ToDoTaskContainer : ElementContainer<ToDoTask>
    {
        public const string ElementName = "TASK";


        protected ToDoTaskContainer()
            : base(ElementName)
        {
        }

        protected ToDoTaskContainer(XmlDocument doc, XmlElement element)
            : base(doc, element, ElementName)
        {
        }


        public ToDoTask CreateTask()
        {
            XmlElement subElement = Doc.CreateElement(SubElementName);
            ToDoTask task = new ToDoTask(Doc, subElement);
            task.Title = "";
            task.Id = "";
            // Position index is 1-based.
            task.Position = Items.Count + 1;

            task.CreationDate = DateTime.Now;
            task.HighestPriority = 10;
            task.LastModified = DateTime.Now;
            task.PercentDone = 0;
            task.Priority = 5;
            task.PriorityColorString = "";
            task.PriorityWebColorString = "";
            task.Risk = "5";
            task.StartDate = DateTime.Now;
            return task;
        }

        public void Sort()
        {
            Items.Sort(new Comparison<ToDoTask>(Sort));
        }

        public void Sort(SortType sortType)
        {
            SortType = sortType;
            Sort();
        }

        private int Sort(ToDoTask item1, ToDoTask item2)
        {
            int result;
            switch (SortType)
            {
                case SortType.Position:
                    result = item1.Position.Value.CompareTo(item2.Position.Value);
                    break;
                case SortType.Priority:
                    result = item1.Priority.Value.CompareTo(item2.Priority.Value);
                    break;
                case SortType.Title:
                    result = item1.Title.CompareTo(item2.Title);
                    break;
                default: result = 0; break;
            }
            return result;
        }
    }
}
